﻿package net.wangit.jdbc.util;

import net.wangit.jdbc.DB;
import net.wangit.jdbc.DBFactory;
import net.wangit.jdbc.DBImplementor;
import net.wangit.jdbc.JDBCException;
import net.wangit.jdbc.ServerAccessException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 数据库帮助类
 * 
 * @author watson
 * Feb 12, 2010
 */
public class DBHelper extends DBFactory {

	private static final Log log = LogFactory
			.getLog(net.wangit.jdbc.util.DBHelper.class);

	public DBHelper() {
	}

	public static DBImplementor getDomainDB(String domain) {
		try {
			return getDomainDB(domain, true);
		} catch (Exception e) {
			throw new ServerAccessException(
					(new StringBuilder("could not connect to server: "))
							.append(domain).toString(), e);
		}
	}

	public static DBImplementor newDomainDB(String domain) {
		try {
			return getDomainDB(domain, false);
		} catch (Exception e) {
			throw new ServerAccessException(
					(new StringBuilder("could not connect to server: "))
							.append(domain).toString(), e);
		}
	}

	public static void closeAll() throws JDBCException {
		closeAllDB();
	}

	public static void close(DB db) {
		try {
			if (db != null)
				db.close();
		} catch (Exception e) {
			log.error((new StringBuilder("log db close error:")).append(e)
					.toString());
		}
	}

}
